
package com.wzh.action.control.collect;


import com.opensymphony.xwork2.ActionContext;
import com.wzh.bean.QueryResult;
import com.wzh.bean.book.BookInfo;
import com.wzh.bean.collect.SiteInfo;
import com.wzh.service.bookinfo.BookInfoService;
import com.wzh.service.bookinfo.chapter.BookChapterService;
import com.wzh.service.siteinfo.SiteInfoService;
import com.wzh.utils.BookCollectUtil;
import com.wzh.utils.CommonUtil;

import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

// /\* [1-9 ]\d+ \*/

@Controller
public class CollectUpdateAction {


    @Resource
    private BookInfoService bookService;


    @Resource
    private BookChapterService chapterService;


    @Resource
    private SiteInfoService siteInfoService;
    private SiteInfo siteinfo;
    private int iscollectchapter = 0;

    private int maxresult = 5;


    public int getIscollectchapter() {

        return this.iscollectchapter;

    }


    public void setIscollectchapter(int iscollectchapter) {

        this.iscollectchapter = iscollectchapter;

    }


    public SiteInfo getSiteinfo() {

        return this.siteinfo;

    }


    public void setSiteinfo(SiteInfo siteinfo) {

        this.siteinfo = siteinfo;

    }


    public String startUpdate() {

        this.siteinfo = ((SiteInfo) this.siteInfoService.find(this.siteinfo.getId()));

        this.siteInfoService.replaceContent(this.siteinfo);

        ActionContext.getContext().getSession().put("siteinfo", this.siteinfo);


        if (this.iscollectchapter > 0) {

            ActionContext.getContext().put("message", "3秒后直接采集未采集的章节内容");

            ActionContext.getContext().put("urladdress", "/control/collect/collectchapter");

        } else {

            ActionContext.getContext().put("message", "3秒后自动更新");

            ActionContext.getContext().put("urladdress", "/control/collect/update/updatebook");

        }


        return "automessage";

    }


    public String updateBook() {

        this.siteinfo = ((SiteInfo) ActionContext.getContext().getSession().get("siteinfo"));

        List bookInfos =
                (List) ActionContext.getContext().getSession().get("bookInfos");

        Integer page = (Integer) ActionContext.getContext().getSession().get("page");

        System.out.println("---------------------" + 1);

        if (page == null) {

            page = Integer.valueOf(0);

        }

        if ((bookInfos == null) || (bookInfos.size() == 0)) {
            System.out.println("---------------------" + 2);


            page = Integer.valueOf(page.intValue() + 1);
            int firstindex = (page.intValue() - 1) * this.maxresult;

            bookInfos = this.bookService.getScrollData(firstindex, this.maxresult,
                    "o.siteid=?1 and o.isfinish=?2", new Object[]{this.siteinfo.getId(), Boolean.valueOf(false)}).getResultlist();


            if ((bookInfos == null) || (bookInfos.size() == 0)) {

                ActionContext.getContext().put("message", "恭喜，所有小说更新完毕,点击确定更新章节内容");

                ActionContext.getContext().put("urladdress", "/control/collect/collectchapter");

            } else {

                ActionContext.getContext().put("message", "3秒后自动开始更新");

                ActionContext.getContext().put("urladdress", "/control/collect/update/updatebook");

            }


        } else {
            System.out.println("---------------------" + 3);


            BookInfo bookinfo = (BookInfo) bookInfos.get(0);


            if (this.siteinfo == null) {

                this.siteinfo = ((SiteInfo) this.siteInfoService.find(bookinfo.getSiteid()));

                ActionContext.getContext().getSession().put("siteinfo", this.siteinfo);

            }

            String srcStr = BookCollectUtil.getWebContentByUrl(bookinfo.getResourceUrl(), this.siteinfo.getCharset());


            String updatetime = BookCollectUtil.getContentByReg(this.siteinfo.getUpdatetimeReg(), srcStr);


            if ((updatetime != null) && (!"".equals(updatetime))) {
                System.out.println("---------------------" + 4);


                Date newUpdatetime = CommonUtil.stringToDate(updatetime);


                if ((newUpdatetime != null) && (newUpdatetime.compareTo(bookinfo.getUpdatetime()) > 0)) {
                    System.out.println("---------------------" + 5);


                    bookinfo.setUpdatetime(newUpdatetime);


                    String bookstate = BookCollectUtil.getContentByReg(this.siteinfo.getStateReg(), srcStr);

                    if ("已完结".equals(bookstate)) {

                        bookinfo.setIsfinish(true);

                    }


                    this.bookService.update(bookinfo);


                    BookCollectUtil.srcStr = srcStr;

                    Map chapterTitlesAndUrls =
                            BookCollectUtil.getChapterTitleAndUrl(this.siteinfo.getChaptertitleAndUrlReg(),
                                    this.siteInfoService.getListOldStr(this.siteinfo),
                                    this.siteInfoService.getListNewStr(this.siteinfo), bookinfo.getResourceUrl().replace("http://", "") + "/", this.siteinfo.getCharset());// this.siteinfo.getDomain()


                    this.chapterService.addChaptersByMap(chapterTitlesAndUrls, bookinfo.getId(), this.siteinfo.getId());

                }


            }


            bookInfos.remove(0);

            ActionContext.getContext().put("message", bookinfo.getTitle() + "更新完毕，3秒后进入下一部小说的更新");

            ActionContext.getContext().put("urladdress", "/control/collect/update/updatebook");

        }

        ActionContext.getContext().getSession().put("bookInfos", bookInfos);

        ActionContext.getContext().getSession().put("page", page);
        System.out.println("---------------------" + 6);

        return "automessage";

    }

}

/* Location:           C:\work\apache-tomcat-7.0.55\webapps\ROOT\WEB-INF\classes\
 * Qualified Name:     com.wzh.action.control.collect.CollectUpdateAction
 * JD-Core Version:    0.6.1
 */